package com.study.bean.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;

import java.util.Arrays;

/**
 * 切面类
 * @Aspect 告诉spring 当前类是切面类
 *
 */
@Aspect
public class LogAspects {
    //抽取公共的切入点表达式
    //1.本类引用
    //2.其他的切面使用
    @Pointcut("execution(public int com.study.bean.aop.MathCalculator.*(..))")
    public void pointCut(){}

    @Before("pointCut()")
    public void logStart(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();//参数列表
        System.out.println(joinPoint.getSignature().getName()+"        方法执行之前 MathCalculator.div .. Before 参数列表是{"+ Arrays.asList(args)+"}");
    }
    @After("pointCut()")
    public void logEnd(JoinPoint joinPoint){
        System.out.println(joinPoint.getSignature().getName()+"        方法结束 MathCalculator.div .. After");
    }
    @AfterReturning(value="pointCut()",returning = "result")//value="用来指定切入点表达式",returning = "用来指定用来接收返回值的变量"
    public void logReturn(JoinPoint joinPoint,Object result){//JoinPoint joinPoint一定要在参数的第一位，否则 spring 无法识别
        System.out.println(joinPoint.getSignature().getName()+"        正常返回 MathCalculator.div .. AfterReturning 运行结果{"+result+"}");
    }
    @AfterThrowing(value="pointCut()",throwing = "exception")
    public void logException(JoinPoint joinPoint,Exception exception){
        System.out.println(joinPoint.getSignature().getName()+"        MathCalculator.div .. AfterThrowing 异常信息{"+exception+"}");
    }

}
